<?php
require_once ('Config/config.php');
require_once ('Swift/lib/swift_required.php');

/**
 * Send Mail
 */
class SendMail {
	// mail info
	private $result       = false;
	private $smtpHost     = SMTP_HOST;
	private $smtpPort 	  = SMTP_PORT;
	private $smtpSSL 	  = SMTP_SSL;
	private $smtpUsername = SMTP_USER;
	private $smtpPassword = SMTP_PASS;
	private $smtpAuth 	  = SMTP_AUTH;

	function __construct() {
	}

    /**
	 * Send Mail
	 * @param String $from, String $to, String $subject, String $body
	 * @return
	 */
	public function sendMail($from, $to, $subject, $body, $toBCC = null)
	{
		$arrSMTPConfg = array(
                'host'     => $this->smtpHost,
                'port'     => $this->smtpPort,
                'auth'     => $this->smtpAuth,
                'ssl'      => $this->smtpSSL,
                'username' => $this->smtpUsername,
                'password' => $this->smtpPassword);
		$charset = "utf-8";
		$transport = Swift_SmtpTransport::newInstance(
				$arrSMTPConfg['host'],
				$arrSMTPConfg['port'],
				$arrSMTPConfg['ssl'])
				->setUsername($arrSMTPConfg['username'])
				->setPassword($arrSMTPConfg['password']);
		Swift_Preferences::getInstance()->setCharset($charset);
		$mailer = Swift_Mailer::newInstance($transport);
		$message = Swift_Message::newInstance($subject)
		->setCharset($charset)
		->setFrom(array($from => $from))
		->setTo(array($to))
		->setBody(str_replace('\n', "\n", $body));
		
		// #8139 >>
		// Using addBcc() to add private recipients
		$toBCC !== null && $toBCC !== '' ? $message->addBcc($toBCC) : '';
		// #8139 <<

		$res = $mailer->send($message);
		return $res;
	}

	/**
	 * send mail with attach pdf file
	 * @param string $to
	 * @param string $subject
	 * @param string $body
	 * @param string $filePath
	 * @param string $fileName
	 * @return number
	 */
	public function sendMailAttachFilePdf($to, $subject, $body, $filePath, $fileName, $toBCC = null) {
			$arrSMTPConfg = array(
					'host'     => $this->smtpHost,
					'port'     => $this->smtpPort,
					'auth'     => $this->smtpAuth,
					'ssl'      => $this->smtpSSL,
					'username' => $this->smtpUsername,
					'password' => $this->smtpPassword);
			$charset = "UTF-8";
			$transport = Swift_SmtpTransport::newInstance(
					$arrSMTPConfg['host'],
					$arrSMTPConfg['port'],
					$arrSMTPConfg['ssl'])
					->setUsername($arrSMTPConfg['username'])
					->setPassword($arrSMTPConfg['password']);
			Swift_Preferences::getInstance()->setCharset($charset);
			$mailer = Swift_Mailer::newInstance($transport);
			$attachment = Swift_Attachment::fromPath($filePath)
			->setFilename(mb_convert_encoding($fileName, 'UTF-8','auto'))
			->setContentType('application/pdf; charset=UTF-8');

			$message = Swift_Message::newInstance($subject)
			->setCharset($charset)
			->setFrom(array(MAIL_FROM))
			->setTo(array($to))
			->setBody(str_replace('\n', "\n", $body));
			$message->attach($attachment);
			
			// #8139 >>
			// Using addBcc() to add private recipients
			$toBCC !== null && $toBCC !== '' ? $message->addBcc($toBCC) : '';	
			// #8139 <<
	
			$res = $mailer->send($message);
			return $res;
	}
}